package com.example.hotelsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.hotelsystem.entity.City;
import com.example.hotelsystem.entity.HotelName;
import com.example.hotelsystem.entity.vo.HotelNameSearchVO;
import com.example.hotelsystem.mapper.HotelNameMapper;
import com.example.hotelsystem.page.Pagesize;
import com.example.hotelsystem.service.CityService;
import com.example.hotelsystem.service.HotelNameService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.hotelsystem.vo.list.ListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Lee
 * @since 2021-03-07
 */
@Service
public class HotelNameServiceImpl extends ServiceImpl<HotelNameMapper, HotelName> implements HotelNameService {

    @Autowired
    HotelNameService hotelNameService;
    @Autowired
    HotelNameMapper hotelNameMapper;
    @Autowired
    CityService cityService;

    @Override
    @Transactional
    public List<ListVO> hotelListVO(Long cityId){
        List<ListVO> list = hotelNameMapper.hotelListVO(cityId);
        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public IPage<HotelName> HotelSearchList(HotelNameSearchVO hotelNameSearchVO){
        Page page = new Page(hotelNameSearchVO.getCurrentPage(), Pagesize.pageSize1);
        QueryWrapper<HotelName> queryWrapper = new QueryWrapper<>();
        if(hotelNameSearchVO.getHotelName()!=null){
            queryWrapper.like("hotel_name",hotelNameSearchVO.getHotelName());
        }
        if (hotelNameSearchVO.getCityId()!=null){
            queryWrapper.like("city_id",hotelNameSearchVO.getCityId());
        }
        queryWrapper.like("is_delete",0);
        IPage<HotelName> hotels=hotelNameService.page(page,queryWrapper);
        for(HotelName hotelName : hotels.getRecords()){
            City city = cityService.getById(hotelName.getCityId());
            hotelName.setCityName(city.getCityName());
        }
        return hotels;
    }
}
